package com.agent.web.report;

import com.agent.entity.RepaireInfo;
import com.agent.page.EasyuiPage;
import com.agent.service.IRepaireInfoService;
import com.agent.shiro.ShiroUser;
import com.agent.util.DateUtil;
import com.agent.vo.RepaireInfoVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 工单流水
 * Created by hg on 2019/1/25.
 */
@RestController
@RequestMapping(value = "/report")
public class WorkOrderSerialController {

    @Resource(name = "repaireInfoService")
    IRepaireInfoService repaireInfoService;


    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    @RequiresRoles(value = {"管理员","售后坐席","售后维护人员"},logical = Logical.OR)
    public EasyuiPage list(Integer page, Integer rows, RepaireInfoVO repaireInfoVO){
        ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Integer roleId = shiroUser.getRoleId();
        // 管理员查询所有的数据
        if (roleId == 1){ //管理员
            if("04".equals(repaireInfoVO.getAddress())){
                repaireInfoVO.setAddress(null);
                repaireInfoVO.setOtherAddress("04");
            }
        }else if (roleId == 2){ //座席
            repaireInfoVO.setAllocationer(shiroUser.getUserName());
        }else if (roleId == 3){ //售后工程师
            repaireInfoVO.setCurrentId(shiroUser.getUserName());
        }
        //2018-10-02 00:00:00 - 2018-11-20 00:00:00
        if(!StringUtils.isEmpty(repaireInfoVO.getRingTime())){
            String[] split = repaireInfoVO.getRingTime().split(" - ");
            repaireInfoVO.setBeginDate(DateUtil.parseDate(split[0],DateUtil.DATE_TIME_FORMAT));
            repaireInfoVO.setEndDate(DateUtil.parseDate(split[1],DateUtil.DATE_TIME_FORMAT));
        }
        EasyuiPage<RepaireInfo> result = repaireInfoService.searchRepaireInfoForReport(page, rows, repaireInfoVO);
        return result;
    }

}
